<?php

/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository
 * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 */

namespace Application\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\Form\Annotation\AnnotationBuilder;
use Application\Form\LoginForm;
use Admin\Model\DBO\UserDBO;
use Admin\Model\DBO\ArticleDBO;

class IndexController extends AbstractActionController {

    protected $form;
    protected $storage;
    protected $authservice;

    public function getForm() {
        if (!$this->form) {
            $user = new LoginForm();
            $builder = new AnnotationBuilder();
            $this->form = $builder->createForm($user);
        }

        return $this->form;
    }

    public function getAuthService() {
        if (!$this->authservice) {
            $this->authservice = $this->getServiceLocator()
                    ->get('AuthService');
        }

        return $this->authservice;
    }

    public function getSessionStorage() {
        if (!$this->storage) {
            $this->storage = $this->getServiceLocator()
                    ->get('Admin\Model\MyAuthStorage');
        }

        return $this->storage;
    }

    public function indexAction() {
        $form = $this->getForm();
        $identity = "";
        if ($this->getAuthService()->hasIdentity()) {
            $identity = $this->getAuthService()->getIdentity();
            $objectUser = new UserDBO($this->getServiceLocator());
            $userInfo = $objectUser->getUserID($identity);
            if (isset($userInfo) && $userInfo['roleID'] < 4) {
                $this->getServiceLocator()->get('Admin\Model\MyAuthStorage')->forgetMe();
                $this->getServiceLocator()->get('AuthService')->clearIdentity();
                  return $this->redirect()->toRoute("home");
            }
        }
        // Lấy 3 tin mới nhất
        $objectArticleDBO = new ArticleDBO($this->getServiceLocator());
        $newArticle = $objectArticleDBO->getNewArticle(1, 1, 3);
        // Lấy 4 thông báo mới nhất
        $announcement = $objectArticleDBO->getNewArticle(1, 2, 3);
        return array(
            'form' => $form,
            'identity' => $identity,
            'newArtcile' => $newArticle,
            'announcement' => $announcement,
        );
    }

    public function authenticateAction() {
        $form = $this->getForm();
        $redirect = 'home';
        $request = $this->getRequest();
        if ($request->isPost()) {
            $form->setData($request->getPost());
            if ($form->isValid()) {
                //check authentication...
                $this->getAuthService()->getAdapter()
                        ->setIdentity($request->getPost('userName'))
                        ->setCredential($request->getPost('password'));
                $result = $this->getAuthService()->authenticate();
                foreach ($result->getMessages() as $message) {
                    //save message temporary into flashmessenger
                    $this->flashmessenger()->addMessage($message);
                }
                if ($result->isValid()) {
                    $redirect = 'personal';
                    // lay roleID
                    //check if it has rememberMe :
                    $this->getAuthService()->setStorage($this->getSessionStorage());
                    $this->getAuthService()->getStorage()->write($request->getPost('userName'));
                }
            }
        }
        return $this->redirect()->toRoute($redirect);
    }

    public function logoutAction() {
        if ($this->getAuthService()->hasIdentity()) {
            $this->getSessionStorage()->forgetMe();
            $this->getAuthService()->clearIdentity();
            $this->flashmessenger()->addMessage("You've been logged out");
        }
        return $this->redirect()->toRoute('home');
    }

    public function checkRole() {
        if (!$this->getAuthService()->hasIdentity()) {
           return $this->redirect()->toRoute('home');
        } else {
            $identity = $this->getServiceLocator()->get('AuthService')->getIdentity();
            $objectUser = new UserDBO($this->getServiceLocator());
            $userInfo = $objectUser->getUserID($identity);
            if (isset($userInfo) && $userInfo['roleID'] < 4) {
                $this->getServiceLocator()->get('Admin\Model\MyAuthStorage')->forgetMe();
                $this->getServiceLocator()->get('AuthService')->clearIdentity();
               return $this->redirect()->toRoute('admin');
            }
        }
        return true;
    }

}
